﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace IRUM.TR
{
    //지수선물 마스터 조회
    public class ProdCode
    {
        public string shcode = string.Empty;
        public string hname = string.Empty;
        public string expcode = string.Empty;
        public string expire = string.Empty;
    }

    public class ProdMasterArgs : EventArgs
    {
        public List<ProdCode> RetData = null;

        public ProdMasterArgs(List<ProdCode> data)
        {
            RetData = data;
        }
    }

    public delegate void ProdMasterHandler(object s, ProdMasterArgs e);

    public class t9943 : CBaseQuery
    {
        public event ProdMasterHandler OnReceived;
        private List<ProdCode> m_list = new List<ProdCode>();

        public t9943()
        {
            SetClassData(TR_Type.t9943);
        }

        public void SetOutBlockData(int Index)
        {
            ProdCode CodeData = new ProdCode();
            string shcode = m_Query.GetFieldData(m_OutBlockName, "shcode", Index);

            // 스프레드는 저장하지 않는다.
            // 1:지수선물, 2:콜옵션, 3:풋옵션, 4:스프레드
            if (shcode[0].Equals('4') == true)
            {
                return;
            }

            string hname = m_Query.GetFieldData(m_OutBlockName, "hname", Index);

            CodeData.hname = hname;
            CodeData.shcode = shcode;
            CodeData.expcode = m_Query.GetFieldData(m_OutBlockName, "expcode", Index);
            string year = shcode.Substring(3, 1);
            string yyyy = string.Empty;

            //6: 1996, 0: 2000, 5: 2005, A: 2006, B: 2007 ... 알파벳 I, O, U는 숫자와 헤깔릴 수 있어서 사용하지 않는다 
            switch (year)
            {
                case "H":
                    yyyy = "2013";
                    break;
                case "J":
                    yyyy = "2014";
                    break;
                case "K":
                    yyyy = "2015";
                    break;
            }

            CodeData.expire = yyyy + hname.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)[1].Replace("월물", "");


            m_list.Add(CodeData);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlockName);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            if (OnReceived != null)
            {
                OnReceived(this, new ProdMasterArgs(m_list));
            }
        }

        protected override void SetRequestData()
        {
            //m_QueryCtrl.m_Query.SetFieldData(m_InBlockName, "dummy", 0, " ");
        }
    }
}
